package com.niuxiaofa.newlaw.adapter;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bytedance.sdk.openadsdk.TTDrawFeedAd;
import com.bytedance.sdk.openadsdk.TTNativeAd;
import com.niuxiaofa.newlaw.Bean.VideoListBean;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.FullScreenVideoView;
import com.niuxiaofa.newlaw.Tools.GetLog;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.video.JzvdStdTikTok;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;


public class SmaillAdapter extends RecyclerView.Adapter<SmaillAdapter.ViewHolder> {
    public List<VideoListBean.DataDTO.ListDTO> listEntities;
    Onclick onclick;


    public SmaillAdapter(List<VideoListBean.DataDTO.ListDTO> newsList, Onclick onclick) {
        this.onclick = onclick;
        this.listEntities = newsList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_tiktok, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        VideoListBean.DataDTO.ListDTO dataDTO = listEntities.get(i);

        //视频广告
        if (dataDTO.getTtDrawFeedAd() != null) {
//            View view = new View(mContext);

//            final VideoView videoView = (VideoView) viewHolder.advideoView.getChildAt(0);
//            final MediaPlayer[] mediaPlayer = new MediaPlayer[1];
//            Log.e("item.videoId",dataDTO.getTtDrawFeedAd().getAdView());

//            viewHolder.advideoView.removeAllViews();
           //  ((VideoView) view).setVideoURI(Uri.parse("android.resource://" + UIUtils.getActivity().getPackageName() + "/" + 0));
        //    viewHolder.advideoView.addView(dataDTO.getTtDrawFeedAd().getAdView());

          initAdViewAndAction(dataDTO.getTtDrawFeedAd(),viewHolder.advideoView);

            dataDTO.setUrl(dataDTO.getTtDrawFeedAd().getCustomVideo().getVideoUrl());
            dataDTO.setCoverImageUrl(dataDTO.getTtDrawFeedAd().getVideoCoverImage().getImageUrl());

            //  view = getView();
            //   ((VideoView) view).setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + item.videoId));
        }

            //普通视频
            Glide.with(viewHolder.jzvdstd.getContext()).load(dataDTO.getCoverImageUrl()).into(viewHolder.jzvdstd.posterImageView);
            JZDataSource jzDataSource = new JZDataSource(dataDTO.getUrl());
            jzDataSource.looping = true;
            viewHolder.jzvdstd.setUp(jzDataSource, Jzvd.SCREEN_NORMAL);

            viewHolder.comment.setOnClickListener(e -> {
                onclick.onclick(i);
            });


    }
    private void initAdViewAndAction(TTDrawFeedAd ad, FrameLayout container) {
        View actionView = UIUtils.getActivity().getLayoutInflater().inflate(R.layout.native_draw_action_item, null, false);
        TextView title = actionView.findViewById(R.id.tv_title);
        title.setText(ad.getTitle());

        TextView desc = actionView.findViewById(R.id.tv_desc);
        desc.setText(ad.getDescription());

        Button action = actionView.findViewById(R.id.button_creative);
        action.setText(ad.getButtonText());

        int margin =20;
        FrameLayout.LayoutParams lp1 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp1.gravity = Gravity.START | Gravity.BOTTOM;
        lp1.leftMargin = margin;
        lp1.bottomMargin = margin;
        container.addView(actionView, lp1);

        List<View> clickViews = new ArrayList<>();
        clickViews.add(title);
        clickViews.add(desc);
        List<View> creativeViews = new ArrayList<>();
        creativeViews.add(action);
        ad.registerViewForInteraction(container, clickViews, creativeViews, new TTNativeAd.AdInteractionListener() {
            @Override
            public void onAdClicked(View view, TTNativeAd ad) {
                GetLog.getLog("onAdClicked");
            }

            @Override
            public void onAdCreativeClick(View view, TTNativeAd ad) {
                GetLog.getLog("onAdCreativeClick");
            }

            @Override
            public void onAdShow(TTNativeAd ad) {
                GetLog.getLog("onAdShow");
            }
        });


    }

    private View getView() {
        FullScreenVideoView videoView = new FullScreenVideoView(UIUtils.getContext());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        videoView.setLayoutParams(layoutParams);
        return videoView;
    }


    @Override
    public int getItemCount() {
        return listEntities.size();
    }

    public interface Onclick {

        void onclick(int c);

    }

    static class ViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.videoplayer)
        JzvdStdTikTok jzvdstd;


        @BindView(R.id.comment)
        TextView comment;

        @BindView(R.id.ad_video)
        FrameLayout advideoView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
